﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using DuJianliang.ApplicationPlatform.Common;
using DuJianliang.ApplicationPlatform.UserPermission;

namespace DuJianliang.ApplicationPlatform.MaterialRequisition
{
    public partial class SelectDispatchMissileProcess : System.Web.UI.Page
    {
        private string DispatchMissileID
        {
            get 
            {
                return ViewState["DispatchMissileID"] != null ? ViewState["DispatchMissileID"].ToString() : string.Empty;
            }
            set
            {
                ViewState["DispatchMissileID"] = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                this.DispatchMissileID = Request.QueryString["DispatchMissileID"];
                BindGridView();
            }
        }

        private void BindGridView()
        {
            string sql = @"SELECT * FROM dbo.t5715_DispatchMissileProcess WHERE DispatchMissileID = @DispatchMissileID";

            Database database = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = database.GetSqlStringCommand(sql);
            database.AddInParameter(dbCommand, "@DispatchMissileID", DbType.String, this.DispatchMissileID);
            DataTable dataTable = database.ExecuteDataSet(dbCommand).Tables[0];
            
            gridView.DataSource = dataTable;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindGridView();
        }

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "CmdSelect":
                    GridViewRow gridViewRow = ((GridViewRow)(((Button)(e.CommandSource)).Parent.Parent));
                    string processSortNumber = gridView.DataKeys[gridViewRow.RowIndex]["ProcessSortNumber"].ToString();

                    Response.Write("<script>window.returnValue='" + processSortNumber + "';window.opener = null;window.open('', '_self', '');window.close();</script>");
                    break;
                default:
                    break;
            }
        }
    }
}